package com.beiding.delaytask.fram.impl;

import com.alibaba.fastjson.JSONObject;
import com.beiding.delaytask.entity.TimeTask;
import com.beiding.delaytask.fram.DelaySender;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author 丁常磊
 * @date 2021/8/12 16:39
 */

@Component
public class DelaySenderImpl implements DelaySender {

    @Value("${delay-task.namespace:delay-task}")
    private String namespace;


    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendDelay(TimeTask timeTask, int i) {
        rabbitTemplate.convertAndSend(namespace+".binary-queue-" + i, JSONObject.toJSONString(timeTask));
    }

    public void sendOnTime(TimeTask timeTask) {
        if (timeTask.getExchange() != null) {
            rabbitTemplate.convertAndSend(timeTask.getExchange(),timeTask.getRoutingKey(),JSONObject.toJSONString(timeTask));
        } else {
            rabbitTemplate.convertAndSend(timeTask.getRoutingKey(), JSONObject.toJSONString(timeTask));
        }
    }


}
